package com.fyl.oauth2.domain;
import org.springframework.security.oauth2.core.AuthorizationGrantType;
import org.springframework.security.oauth2.core.ClientAuthenticationMethod;
import org.springframework.security.oauth2.server.authorization.client.RegisteredClient;
import org.springframework.security.oauth2.server.authorization.client.RegisteredClient.Builder;
import org.springframework.security.oauth2.server.authorization.settings.ClientSettings;
import java.util.Set;
import java.util.UUID;

public class ClientEntity {
    private Long id;
    private String clientId;
    private String clientSecret;
    private String scopes; // 逗号分隔的字符串
    private String redirectUris; // 逗号分隔的字符串
    private String authorizationGrantTypes; // 逗号分隔的字符串
    private String clientAuthenticationMethods; // 逗号分隔的字符串

    // Getters and setters

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getClientId() {
        return clientId;
    }

    public void setClientId(String clientId) {
        this.clientId = clientId;
    }

    public String getClientSecret() {
        return clientSecret;
    }

    public void setClientSecret(String clientSecret) {
        this.clientSecret = clientSecret;
    }

    public String getScopes() {
        return scopes;
    }

    public void setScopes(String scopes) {
        this.scopes = scopes;
    }

    public String getRedirectUris() {
        return redirectUris;
    }

    public void setRedirectUris(String redirectUris) {
        this.redirectUris = redirectUris;
    }

    public String getAuthorizationGrantTypes() {
        return authorizationGrantTypes;
    }

    public void setAuthorizationGrantTypes(String authorizationGrantTypes) {
        this.authorizationGrantTypes = authorizationGrantTypes;
    }

    public String getClientAuthenticationMethods() {
        return clientAuthenticationMethods;
    }

    public void setClientAuthenticationMethods(String clientAuthenticationMethods) {
        this.clientAuthenticationMethods = clientAuthenticationMethods;
    }

    public static RegisteredClient toRegisteredClient(ClientEntity entity) {
        if (entity == null) return null;

        Builder builder = RegisteredClient.withId(entity.getId().toString())
                .clientId(entity.getClientId())
                .clientSecret(entity.getClientSecret())
                .clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC)
                .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
                .authorizationGrantType(AuthorizationGrantType.REFRESH_TOKEN)
                .redirectUri(entity.getRedirectUris().split(",")[0])
                .scope(entity.getScopes().split(",")[0])
                .clientSettings(ClientSettings.builder().requireAuthorizationConsent(true).build());

        for (String scope : entity.getScopes().split(",")) {
            builder.scope(scope);
        }

        for (String redirectUri : entity.getRedirectUris().split(",")) {
            builder.redirectUri(redirectUri);
        }

        for (String grantType : entity.getAuthorizationGrantTypes().split(",")) {
//            builder.authorizationGrantType(AuthorizationGrantType.valueOf(grantType));
        }

        for (String authenticationMethod : entity.getClientAuthenticationMethods().split(",")) {
//            builder.clientAuthenticationMethod(ClientAuthenticationMethod.valueOf(authenticationMethod));
        }

        return builder.build();
    }

    public static ClientEntity fromRegisteredClient(RegisteredClient registeredClient) {
        ClientEntity entity = new ClientEntity();
        entity.setId(Long.parseLong(registeredClient.getId()));
        entity.setClientId(registeredClient.getClientId());
        entity.setClientSecret(registeredClient.getClientSecret());
        entity.setScopes(String.join(",", registeredClient.getScopes()));
        entity.setRedirectUris(String.join(",", registeredClient.getRedirectUris()));
        entity.setAuthorizationGrantTypes(String.join(",", registeredClient.getAuthorizationGrantTypes().stream().map(AuthorizationGrantType::getValue).toArray(String[]::new)));
        entity.setClientAuthenticationMethods(String.join(",", registeredClient.getClientAuthenticationMethods().stream().map(ClientAuthenticationMethod::getValue).toArray(String[]::new)));
        return entity;
    }
}
